<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
    Integer datastatus = (Integer) session.getAttribute("datastatus");
    Integer totlenum = (Integer) session.getAttribute("totlenum");
    Integer totalMoney = (Integer) session.getAttribute("totalMoney");
    Integer curPage = (Integer) session.getAttribute("curPage");
    String username = (String) session.getAttribute("username");
    String shopid = (String) session.getAttribute("shopid");
    Integer adminUsergrade = (Integer) session.getAttribute("adminUsergrade");
    String parentid = (String) session.getAttribute("parentid");
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript" src="<%=basePath%>js/initPage.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>随便打TCS系统</title>

    <!-- Bootstrap -->
    <link href="<%=basePath%>vendors/bootstrap/dist/css/bootstrap.min.css"
          rel="stylesheet">
    <!-- Font Awesome -->
    <link
            href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
            rel="stylesheet">
    <!-- NProgress -->
    <link href="<%=basePath%>vendors/nprogress/nprogress.css"
          rel="stylesheet">
    <!-- iCheck -->
    <link href="<%=basePath%>vendors/iCheck/skins/flat/green.css"
          rel="stylesheet">

    <!-- bootstrap-progressbar -->
    <link
            href="<%=basePath%>vendors/bootstrap-progressbar/css/bootstrap-progressbar-3.3.4.min.css"
            rel="stylesheet">
    <!-- JQVMap -->
    <link href="<%=basePath%>vendors/jqvmap/dist/jqvmap.min.css"
          rel="stylesheet"/>
    <!-- bootstrap-daterangepicker -->
    <link
            href="<%=basePath%>vendors/bootstrap-daterangepicker/daterangepicker.css"
            rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="<%=basePath%>build/css/custom.min.css" rel="stylesheet">
</head>

<body class="nav-md">
<div class="container body">
    <div class="main_container">
        <div class="col-md-3 left_col">
            <div class="left_col scroll-view">

                <div class="clearfix"></div>

                <br/>

            </div>
        </div>

        <!-- page content -->
        <div class="right_col" role="main">
            <div class="">
                <div class="page-title">
                    <div class="title_left"></div>

                    <input type="hidden" name="parent" id="adminUserId"
                           value="${adminUserId}"/>

                    <div class="x_content">
                        <c:if test="${datastatus !=1 && datastatus !=2}">
                            <select class="btn btn-default dropdown-toggle" id="datastatus" style="text-align: center;">
                                <option value="0" selected="selected"> 全部</option>
                                <option value="1">可用</option>
                                <option value="2">冻结</option>
                            </select>
                        </c:if>
                        <c:if test="${datastatus == 1}">
                            <select class="btn btn-default dropdown-toggle" id="datastatus" style="text-align: center;">
                                <option value="0"> 全部</option>
                                <option value="1" selected="selected">可用</option>
                                <option value="2">冻结</option>
                            </select>
                        </c:if>
                        <c:if test="${datastatus == 2}">
                            <select class="btn btn-default dropdown-toggle" id="datastatus" style="text-align: center;">
                                <option value="0"> 全部</option>
                                <option value="1">可用</option>
                                <option value="2" selected="selected">冻结</option>
                            </select>
                        </c:if>
                        商家账号: <input type="text" name="lname" id="username"
                                     value="${username}"/>
                        <c:if test="${adminUsergrade < 2  }">
                            所属OEM: <input type="text" name="lname" id="parentid"
                                          value="${parentid}"/>
                            所属商家: <input type="text" name="lname" id="shopid"
                                         value="${shopid}"/>
                        </c:if>
                        <button type="submit" class="btn btn-default"
                                onclick="selectOEM(1)">搜索
                        </button>
                        <c:if test="${adminUser.nextagent == true}">
                            <button type="submit" class="btn btn-success"
                                    onclick="createOem()">添加
                            </button>
                        </c:if>
                        <!-- <button type="submit" class="btn btn-default"
                            onclick="dongjieAll()">批量冻结</button>
                        <button type="submit" class="btn btn-default"
                            onclick="jiedongAll()">批量解冻</button> -->
                        <a>共计${totlenum}商户</a>
                        <c:if test="${adminUsergrade == 1}">
                            <button type="button" class="btn btn-default"
                                    onclick="" id="operation">数据操作
                            </button>
                        </c:if>
                        <button type="submit" class="btn btn-default" style="display: none"
                                onclick="daochu()" id="export">导出查询结果
                        </button>
                        <button type="submit" class="btn btn-success" onclick="selfOrderTotalIndex('${selfOrderTotalShopId}')">总业绩
                            <span>${totalMoney} 万元</span>
                        </button>
                    </div>
                </div>


                <div class="x_panel">

                    <div class="x_content">

                        <table id="datatable" class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th><input type="checkbox" id="checkall" name="checkall"
                                           onclick="checkAll()"/>全选
                                </th>
                                <th>账号</th>
                                <th>密码</th>
                                <th>状态</th>
                                <c:if test="${adminUsergrade < 2  }">
                                    <th>所属OEM</th>
                                    <th>所属商家</th>
                                    <th>系统消耗</th>
                                </c:if>
                                <c:if test="${adminUsergrade < 2  }">
                                    <th>面值余额</th>
                                </c:if>
                                <th>购物余额</th>
                                <th>可用余额</th>
                                <c:if test="${isLTH != 1}">
                                    <th>费率</th>
                                    <c:if test="${adminUsergrade < 2  }">
                                        <th>中继</th>
                                    </c:if>
                                </c:if>
                                <th>注册时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>


                            <tbody>
                            <c:forEach items="${list}" var="s" varStatus="z">
                                <tr>
                                    <td><input type="checkbox" value="${s.id}" id="check"
                                               name="check"/></td>
                                    <td id="username">${s.username }</td>
                                    <td>${s.password }</td>
                                    <td><c:if test="${s.datastatus == true }">
                                        <span>可用</span>
                                    </c:if> <c:if test="${s.datastatus == false }">
                                        <span>冻结</span>
                                    </c:if></td>
                                    <c:if test="${adminUsergrade < 2  }">
                                        <td>${s.parentid}</td>
                                        <td>${s.shopid}</td>
                                        <td>${s.keypath }</td>
                                    </c:if>
                                    <c:if test="${adminUsergrade < 2  }">
                                        <td>${s.blance}</td>
                                    </c:if>
                                    <td>${s.shopblance}</td>
                                    <td>${s.balance}</td>
                                    <c:if test="${isLTH != 1}">
                                        <td>${s.freeroute }</td>
                                        <c:if test="${adminUsergrade < 2  }">
                                            <td><c:if test="${s.calltype == 1 }">
                                                <span>久话</span>
                                            </c:if> <c:if test="${s.calltype == 2 }">
                                                <span>魅逸</span>
                                            </c:if></td>
                                        </c:if>
                                    </c:if>
                                    <td><fmt:formatDate value="${s.createtime}" type="date"
                                                        pattern="yyyy-MM-dd"/></td>
                                    <td><c:if test="${s.datastatus == true }">
                                        <a href="javascript:" type="submit"
                                           class="btn btn-success"
                                           onclick="huazhang('${s.id}','${s.blance}')">划账</a>
                                        <a href="javascript:" type="submit"
                                           class="btn btn-success"
                                           onclick="chongzhigouwu('${s.id}','${s.shopblance}')">充值购物余额</a>
                                        <button type="submit" class="btn btn-success"
                                                onclick="content_xiugaishangjia('${s.id}',2);">修改
                                        </button>
                                        <button type="submit" class="btn btn-default"
                                                onclick="dongjie('${s.id}','${s.username}')">冻结
                                        </button>
                                    </c:if> <c:if test="${s.datastatus == false }">
                                        <button type="submit" class="btn btn-default"
                                                onclick="jiedong('${s.id}','${s.username}')">解冻
                                        </button>
                                    </c:if>
                                        <a href="javascript:" type="submit"
                                           class="btn btn-success"
                                           onclick="content_xiajishangjiaguanli('${s.id}')">下级代理（${s.shopnum}）</a>
                                        <a href="javascript:" type="submit"
                                           class="btn btn-success"
                                           onclick="selectShopUsers('${s.id}')">商家用户（${s.subnum}）</a>
                                        <a href="javascript:" type="submit"
                                           class="btn btn-success"
                                           onclick="content_duizhangdan('${s.id}')">充值数据</a>
                                        <a href="javascript:" type="submit" class="btn btn-success"
                                           onclick="toDialogBox('${s.id}','${s.dialogboxid}')">对话框文字</a>
                                        <a href="javascript:" type="submit" class="btn btn-success"
                                           onclick="selfOrderTotalIndex('${s.id}')">
                                            总业绩 ${s.totalMoney} 万元
                                        </a>
                                    </td>
                                </tr>
                            </c:forEach>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="clearfix"></div>
                <br/>
            </div>
            <div style="width: 100%; text-align: center;">
                <div id='page'></div>
            </div>
        </div>
        <!-- /page content -->


        <script>
            /**
             * 纯粹的JS分页插件，代码缺点：JS操作DOM冗余太多，太繁琐
             */
            pageUtil.initPage('page', {
                totalCount: ${totlenum}, //总页数，一般从回调函数中获取。如果没有数据则默认为1页
                curPage: ${curPage}, //初始化时的默认选中页，默认第一页。如果所填范围溢出或者非数字或者数字字符串，则默认第一页
                showCount: 9, //分页栏显示的数量
                pageSizeList: [10], //自定义分页数，默认[5,10,15,20,50]
                defaultPageSize: 10, //默认选中的分页数,默认选中第一个。如果未匹配到数组或者默认数组中，则也为第一个
                isJump: true, //是否包含跳转功能，默认false
                isPageNum: true, //是否显示分页下拉选择，默认false
                isPN: true, //是否显示上一页和下一面，默认true
                isFL: true, //是否显示首页和末页，默认true
                jump: function (curPage, pageSize) { //跳转功能回调，传递回来2个参数，当前页和每页大小。如果没有设置分页下拉，则第二个参数永远为0。这里的this被指定为一个空对象，如果回调中需用到this请自行使用bind方法
                    selectOEM(curPage);
                },
            });
        </script>


    </div>
</div>

<script src="<%=basePath%>vendors/jquery/dist/jquery.min.js"></script>
<script>
    $(document).ready(function (e) {
        $("#operation").click(function () {
            $("#export").toggle();
        });
    });
</script>
<script type="text/javascript">
    function selectOEM(curPage) {
        var username = $("#username").val();
        var parentid = $("#parentid").val();
        var shopid = $("#shopid").val();
        var datastatus = $("#datastatus").find("option:selected").val();
        location.href = "selectShop?username=" + username +
            "&curPage=" + curPage +
            "&parentid=" + parentid +
            "&shopid=" + shopid +
            "&datastatus=" + datastatus;
    }

    //2 商家管理
    function content_xiugaishangjia(id, choose) {
        location.href = "content_xiugaishangjia?id=" + id +
            "&choose=" + choose;
    }

    function createOem() {
        location.href = "content_tianjiashangjia"
    }

    function selfOrderTotalIndex(shopId){
        location.href = "selfOrder/selfOrderTotalIndex?shopId=" + shopId
    }

    function content_xiajishangjiaguanli(id) {
        location.href = "content_xiajishangjiaguanli?id=" + id;
    }

    function content_duizhangdan(id) {
        location.href = "content_duizhangdan?id=" + id;
    }

    function selectShopUsers(id) {
        location.href = "selectShopUsers?pid=" + id;
    }

    function toDialogBox(userid, dialogboxid) {
        if (dialogboxid != null && dialogboxid != "") {
            location.href = "updateDialogBox?id=" + dialogboxid;
        } else {
            location.href = "addDialogBox?userid=" + userid;
        }
    }

    // ajax 对象
    function ajaxObject() {
        var xmlHttp;
        try {
            // Firefox, Opera 8.0+, Safari
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            // Internet Explorer
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("您的浏览器不支持AJAX！");
                    return false;
                }
            }
        }
        return xmlHttp;
    }

    function dongjie(id, username) {
        var ajax = ajaxObject();
        ajax.open("post", "updateDongjieAndJiedongUsers?id=" + id +
            "&datastatus=" + false, false);
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    if (result == "success") {
                        //请求成功
                        if (username != null) {
                            alert(username + "账号已经冻结");
                            location.href = "content_shangjiaguanli";
                        }
                    } else {
                        //请求成功
                        alert(result);
                    }
                } else {
                    //请求失败
                    alert("请求终止，请检查操作或联系客服咨询" + ajax.status);

                }
            } else {

            }
        };
        ajax.send();
    }

    function jiedong(id, username) {
        var ajax = ajaxObject();
        ajax.open("post", "updateDongjieAndJiedongUsers?id=" + id +
            "&datastatus=" + true, false);
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    if (result == "success") {
                        //请求成功
                        if (username != null) {
                            alert(username + "账号已经解冻");
                            location.href = "content_shangjiaguanli";
                        }
                    } else {
                        //请求成功
                        alert(result);
                    }
                } else {
                    //请求失败
                    alert("请求终止，请检查操作或联系客服咨询" + ajax.status);
                }
            } else {

            }
        };
        ajax.send();
    }

    //划账
    function huazhang(id, blance) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;
            layer.prompt({
                title: '输入充值金额，并确认',
                formType: 3
            }, function (pass, index) {
                var ajax = ajaxObject();
                ajax.open("post", "updateHuaZhangUsers?id=" + id +
                    "&blance=" + blance +
                    "&pass=" + pass, true);
                ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                ajax.onreadystatechange = function () {
                    if (ajax.readyState == 4) {
                        if (ajax.status == 200) {
                            var result = ajax.responseText;
                            if (result == "success") {
                                //请求成功
                                alert("划账成功");
                                location.href = "content_shangjiaguanli";
                            } else {
                                //请求成功
                                alert("划账成功");
                                location.href = "content_shangjiaguanli";
                            }
                        } else {
                            //请求失败
                            alert("请求终止，请检查操作或联系客服咨询" + ajax.status);
                            location.href = "content_shangjiaguanli";
                        }
                    } else {

                    }
                };
                ajax.send();

            });
        });
    }

    //充值购物
    function chongzhigouwu(id, shopblance) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;
            layer.prompt({
                title: '输入充值金额，并确认',
                formType: 3
            }, function (pass, index) {
                var ajax = ajaxObject();
                ajax.open("post", "updateHuaZhangUsers?id=" + id +
                    "&shopblance=" + shopblance +
                    "&pass=" + pass, true);
                ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                ajax.onreadystatechange = function () {
                    if (ajax.readyState == 4) {
                        if (ajax.status == 200) {
                            var result = ajax.responseText;
                            if (result == "success") {
                                //请求成功
                                alert("购物余额充值成功");
                                location.href = "content_shangjiaguanli";
                            } else {
                                //请求成功
                                alert(result);
                                location.href = "content_shangjiaguanli";
                            }
                        } else {
                            //请求失败
                            alert("请求终止，请检查操作或联系客服咨询" + ajax.status);
                        }
                    } else {
                    }
                };
                ajax.send();
            });
        });

    }

    //全选
    function checkAll() {
        var all = document.getElementById('checkall'); //获取到点击全选的那个复选框的id
        var one = document.getElementsByName('check'); //获取到复选框的名称
        if (all.checked == true) { //因为获得的是数组，所以要循环 为每一个checked赋值
            for (var i = 0; i < one.length; i++) {
                one[i].checked = true;
            }

        } else {
            for (var j = 0; j < one.length; j++) {
                one[j].checked = false;
            }
        }
    }


    //批量冻结
    function dongjieAll() {
        var one = document.getElementsByName('check');
        var choose = 0;
        for (var i = 0; i < one.length; i++) {
            if (one[i].checked) {
                dongjie(one[i].value);
                choose = 1;
            }
        }
        if (choose == 1) {
            alert("所选账号已冻结");
            location.href = "content_shangjiaguanli";
        } else {
            alert("请选择需要冻结账号");
        }

    }

    //批量冻结
    function jiedongAll() {
        var one = document.getElementsByName('check');
        var choose = 0;
        for (var i = 0; i < one.length; i++) {
            if (one[i].checked) {
                jiedong(one[i].value);
                choose = 1;

            }

        }
        if (choose == 1) {
            alert("所选账号已解冻");
            location.href = "content_shangjiaguanli";
        } else {
            alert("请选择需要解冻账号");
        }
    }

    function daochu() {
        alert("正在导出中，请稍后……");
        var username = $("#username").val();
        var parentid = $("#parentid").val();
        var shopid = $("#shopid").val();
        var adminUserId = $("#adminUserId").val();
        var datastatus = $("#datastatus").find("option:selected").val();
        var ajax = ajaxObject();

        ajax.open("post", "<%=basePath%>apiTest/shopExport?username=" + username +
            "&parentid=" + parentid +
            "&shopid=" + shopid +
            "&datastatus=" + datastatus +
            "&adminUserId=" + adminUserId, true);
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    alert(result);
                    window.open("http://" + result);
                } else {
                    //请求失败
                    alert("请求终止，请检查操作或联系客服咨询" + ajax.status);
                }
            }
        };
        ajax.send();
    }
</script>
<!-- jQuery -->
<script src="<%=basePath%>vendors/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="<%=basePath%>vendors/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- FastClick -->
<script src="<%=basePath%>vendors/fastclick/lib/fastclick.js"></script>
<!-- NProgress -->
<script src="<%=basePath%>vendors/nprogress/nprogress.js"></script>
<!-- bootstrap-daterangepicker -->
<script src="<%=basePath%>vendors/moment/min/moment.min.js"></script>
<script
        src="<%=basePath%>vendors/bootstrap-daterangepicker/daterangepicker.js"></script>
<!-- Chart.js -->
<script src="<%=basePath%>vendors/Chart.js/dist/Chart.min.js"></script>
<!-- Custom Theme Scripts -->
<script src="<%=basePath%>build/js/custom.min.js"></script>
<!-- layui -->
<script src="<%=basePath%>vendors/layui/layui.js"></script>

</body>
</html>
